package cn.luzhangduo.studentsystemproject.controller;

import cn.luzhangduo.studentsystemproject.model.Agent;
import cn.luzhangduo.studentsystemproject.service.AgentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    private AgentService agentService;
    @RequestMapping("/add")
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView("agent/input");
        return modelAndView;
    }
    @RequestMapping("/addSave")
    public ModelAndView addSave(Agent agent){
        if(agentService.addAgent(agent)){
            return list();
        }else{
            return add().addObject("message","保存失败");
        }
    }
    @RequestMapping("/list")
    public ModelAndView list(){
        List<Agent> agents = agentService.getAgents();
        ModelAndView modelAndView=new ModelAndView();
        //把数据存入数据域模型
        modelAndView.addObject("agents",agents);
        modelAndView.setViewName("agent/list");
        return modelAndView;
    }

    @RequestMapping("/showData")
    public String showData(){
        return "agent/showData";
    }


}
